package com.zany.system.action;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.login.service.LoginService;
import com.zany.system.vo.UserInfoVO;
import com.zany.system.service.PasswordInfoService;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.action.MD5;
import com.zany.util.action.SessionInfo;
import com.zany.util.ajax.AjaxTools;
/**
 * 密码管理
 * @author cy
 * @version 1.0
 */
public class PasswordInfoAction extends AbstractActionSupport{

	private static final long serialVersionUID = -4006691238914898384L;
	private static Log logger = LogFactory.getLog(PasswordInfoAction.class);
	private PasswordInfoService passwordInfoService;
	private LoginService loginService;
	private UserInfoVO userInfoVO = new UserInfoVO();
	
	public void changePassword() throws Exception{
		if(logger.isInfoEnabled()){logger.info("PasswordInfoAction.changePassword()");}
		response = ServletActionContext.getResponse();
		userInfoVO.setUserCode(SessionInfo.getUserCode(request));
		userInfoVO.setPassword(MD5.md5(request.getParameter("passOld")));
		userInfoVO = loginService.login(userInfoVO);
		if(userInfoVO==null){
			AjaxTools.exAjax("{\"success\":false,msg:\"用户名或密码错误！\"}",response);
		}else{
			userInfoVO.setPassword(MD5.md5(request.getParameter("passNew")));
			try{
				passwordInfoService.updatePassword(userInfoVO);
				AjaxTools.exAjax("{success:true}", response);
			}catch (Exception e) {
				AjaxTools.exAjax("{success:false}", response);
				throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
			}
		}
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub		
	}
	public PasswordInfoService getPasswordInfoService() {
		return passwordInfoService;
	}
	public void setPasswordInfoService(PasswordInfoService passwordInfoService) {
		this.passwordInfoService = passwordInfoService;
	}
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}
